package com.ruoyi.project.tickets.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.tickets.domain.ShopUser;
import com.ruoyi.project.tickets.domain.vo.ShopUserVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.tickets.domain.Shop;
import com.ruoyi.project.tickets.service.IShopService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 店铺信息Controller
 * 
 * @author 孙山伟
 * @date 2023-06-28
 */
@RestController
@RequestMapping("/tickets/shop")
public class ShopController extends BaseController
{
    @Autowired
    private IShopService shopService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询店铺信息列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shop shop)
    {
        startPage();
        List<Shop> list = shopService.selectShopList(shop);
        return getDataTable(list);
    }
    @GetMapping("/getUSer/{id}")
    public AjaxResult getUSer(@PathVariable Long id)
    {
        List<ShopUserVo> list = shopService.getUSer(id);
        return AjaxResult.success(list);
    }

    /**
     * 根据当前用户查询所负责的店铺
     * @return
     */
    @GetMapping("/selectByUserId")
    public AjaxResult selectByUserId()
    {
        Long  userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        if (SysUser.isAdmin(userId)){
            userId=null;
        }
        List<Shop> shopList=shopService.selectListByUserId(userId);
        return AjaxResult.success(shopList);
    }
    /**
     * 导出店铺信息列表
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:export')")
    @Log(title = "店铺信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Shop shop)
    {
        List<Shop> list = shopService.selectShopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<Shop>(Shop.class);
        return util.exportExcel(list, "shop");
    }

    /**
     * 获取店铺信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(shopService.selectShopById(id));
    }

    /**
     * 新增店铺信息
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:add')")
    @Log(title = "店铺信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shop shop)
    {
        return toAjax(shopService.insertShop(shop));
    }

    /**
     * 修改店铺信息
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:edit')")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shop shop)
    {
        return toAjax(shopService.updateShop(shop));
    }

    /**
     * 删除店铺信息
     */
    @PreAuthorize("@ss.hasPermi('tickets:shop:remove')")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopService.deleteShopByIds(ids));
    }
}
